package com.aaa.project.controller;


import com.aaa.entity.GGGoods;
import com.aaa.entity.GGoods;
import com.aaa.entity.UCollection;
import com.aaa.entity.UUserInfo;
import com.aaa.project.service.IBBusinessInfoService;
import com.aaa.project.service.IGGoodsService;
import com.aaa.project.service.IUCollectionService;
import com.aaa.project.service.IUUserInfoService;
import com.aaa.project.util.GetBid;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author zhangxuhui
 * @since 2023-02-06
 */
@RestController
@RequestMapping("/g-goods")
public class GGoodsController {

    @Resource
    private IGGoodsService goodsService;
    @Resource
    private IBBusinessInfoService ibBusinessInfoService;
    @Resource
    private IUUserInfoService userInfoService;
    @Resource
    private IUCollectionService iuCollectionService;
    @Resource
    private GetBid getBid;

    /**
     *获取店铺信息以及商品信息
     * @param id
     * @return
     */
    @GetMapping("findGoodsByBid/{id}")
    public Result findGoodsByBid(@PathVariable String  id){
        HashMap<String,Object> map = new HashMap<>();
        List<GGoods> all = goodsService.list(new QueryWrapper<GGoods>().eq("bid",id).eq("isdeleted",0));
        map.put("goodlist",all);
        map.put("business",ibBusinessInfoService.getById(id));
        return new Result<>(map);
    }
    /**
     * 商品成交量
     */
    @PostMapping("getGoodsSellNum/{id}")
    public Result getGoodsSellNum(@PathVariable String id){
        return new Result(goodsService.findAllNum(id));
    }

    /**
     * 商品是否收藏
     * status==0 已经收藏
     * status==1 未收藏
     */
    @PostMapping("isGoodsCollection")
    public Result isGoodsCollection(@RequestParam String id, Principal principal){
        String name = principal.getName();
        // 根据当前的人的名字用户的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=userInfoService.getOne(queryWrapper);
        String uid = uUserInfo.getId();
        goodsService.findGoods(uid,id);
        return new Result(goodsService.findGoods(uid, id));
    }
    /**
     * 修改收藏状态
     * @return
     */
    @PostMapping("changeCollectionStatus")
    public Result changeCollection( UCollection collection,Principal principal){
        String name = principal.getName();
        // 根据当前的人的名字用户的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=userInfoService.getOne(queryWrapper);
        collection.setUid(uUserInfo.getId());
        UpdateWrapper<UCollection> wrapper = new UpdateWrapper<>();
        //wrapper.eq("is_deleted", 0);
        if (collection.getGoodsid() != null) {
            wrapper.eq("goodsid",collection.getGoodsid());
        } else {
            wrapper.eq("bid",collection.getBid());
        }
        wrapper.eq("uid",uUserInfo.getId());
        collection.setIsDeleted(0);
        return new Result<>(iuCollectionService.saveOrUpdate(collection,wrapper));
    }

    /**
     * 商品库存
     * getStock=total_num - sell_num
     */
    @PostMapping("getStock/{id}")
    public Result getStock(@PathVariable String id){
        return new Result<>(goodsService.jiSuan(id));
    }

//    商家
//初始化
@PostMapping("getAllGoodsbyid")
public Result getAllGoods(Integer curr,Integer size,String telephone,GGGoods gGoods){
    Page page = new Page();
    //GetBid getBid = new GetBid();
    String bid = getBid.getbid(telephone);
    gGoods.setBid(bid);
//    System.out.println(page);
    List list = goodsService.getAllgoodsinfoBybid(curr,size,bid,gGoods);
    System.out.println(list);
    Page page2 = page.setRecords(list);
    page2.setCurrent(curr);
    page2.setSize(size);
    return  new Result(page2);

//    return new Result(page1);
}

    //    商品删除
    @PostMapping("deleteGoods")
    public Result deleteGoods(Integer id){
        return new Result(goodsService.removeById(id));
    }
    //    商品上下架  0上架1下架
    @PostMapping("updateStatus")
    public Result updateStatus(GGGoods gGoods){
        return new Result(goodsService.update(gGoods));
    }

    //    商品信息添加
    @PostMapping("addGoods")
    public Result addGoods(GGGoods gGoods,String telephone){
        String bid = getBid.getbid(telephone);
        gGoods.setBid(bid);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = sdf.format(date);
        gGoods.setAddtime(addTime);
        gGoods.setIsdeleted(0);
        gGoods.setState(2);
        gGoods.setStatus(1);
        return  new Result(goodsService.save(gGoods));

    }
    //    商品信息修改
    @PostMapping("updateGoods")
    public Result updateGoods(GGGoods gGoods){
        int a =  goodsService.update(gGoods);
        return new Result(a);
    }

}

